function thisrender(this, varargin)
%RENDER Renders the "Realize Model" panel
%   RENDER Renders the sidebar object associated with hSB.

%   Copyright 1995-2008 The MathWorks, Inc.
%   $Revision: 1.1.6.5 $  $Date: 2008/06/13 15:30:20 $

pos = parserenderinputs(this, varargin{:});

sz  = gui_sizes(this);

if isempty(pos),
    pos = [10 10 710 205] * sz.pixf;
end

hFig = get(this, 'FigureHandle');

h.container = uicontainer('Parent', hFig, ...
    'Units',    'Pixels', ...
    'Position', pos, ...
    'Visible',  'off');
set(h.container, 'units', 'normalized');
hLayout = siglayout.gridbaglayout(h.container);
set(hLayout, 'VerticalWeights', [1 0], ...
    'HorizontalGap', 5*sz.pixf, ...
    'VerticalGap', 5*sz.pixf);

set(this, 'Container', h.container, 'Layout', hLayout);

h.mframe = uipanel('Parent', h.container, ...
    'Title', ' Model ', ...
    'Tag', 'wiz_model');
h.oframe = uipanel('Parent', h.container, ...
    'Title', ' Optimization ', ...
    'Tag', 'wiz_optimization');

hLayout.add(h.mframe, 1, 1, 'Fill', 'Both');
hLayout.add(h.oframe, 1, 2, 'Fill', 'Both');

set(this, 'Handles', h);

renderactionbtn2(this, 2, [1 2], 'Realize Model', 'build');

hLayout.setconstraints(2, 1, 'MinimumHeight', 20*sz.pixf);

rendercontrols(this, h.mframe, {'blockname', 'destination', ...
    'userdefined', 'overwriteblock', 'usebasicelements'});
rendercontrols(this, h.oframe, {'optimizezeros', 'optimizeones', ...
    'optimizenegones', 'optimizedelaychains', 'optimizescalevalues'});

h = get(this, 'Handles');

hLayout = siglayout.gridbaglayout(h.mframe);
set(hLayout, 'HorizontalWeights', [0 1]);
hLayout.add(h.blockname_lbl,    1, 1, ...
    'Anchor', 'southwest', ...
    'MinimumWidth', largestuiwidth(h.blockname_lbl), ...
    'TopInset', 15*sz.pixf+sz.lblTweak, ...
    'LeftInset', 10*sz.pixf);
hLayout.add(h.blockname,        1, 2, ...
    'MinimumWidth', 100*sz.pixf, ...
    'Anchor', 'west', ...
    'Fill', 'Horizontal', ...
    'TopInset', 15*sz.pixf, ...
    'RightInset', 10*sz.pixf);
hLayout.add(h.destination_lbl, 2, 1, ...
    'MinimumWidth', largestuiwidth(h.destination_lbl), ...
    'TopInset', sz.lblTweak, ...
    'Anchor', 'southwest', ...
    'LeftInset', 10*sz.pixf);
hLayout.add(h.destination,     2, 2, ...
    'Anchor', 'west', ...
    'MinimumWidth', 100*sz.pixf, ...
    'RightInset', 10*sz.pixf);
hLayout.add(h.userdefined_lbl, 3, 1, ...
    'Anchor', 'southwest', ...
    'MinimumWidth', largestuiwidth(h.userdefined_lbl), ...
    'TopInset', sz.lblTweak, ...
    'LeftInset', 10*sz.pixf);
hLayout.add(h.userdefined,     3, 2, ...
    'Anchor', 'west', ...
    'Fill', 'Horizontal', ...
    'MinimumWidth', 100*sz.pixf, ...
    'RightInset', 10*sz.pixf);
hLayout.add(h.overwriteblock,   4, [1 2], ...
    'Fill', 'Horizontal', ...
    'LeftInset', 10*sz.pixf, ...
    'RightInset', 10*sz.pixf);
hLayout.add(h.usebasicelements, 5, [1 2], ...
    'Fill', 'Horizontal', ...
    'BottomInset', 5*sz.pixf, ...
    'LeftInset', 10*sz.pixf, ...
    'RightInset', 10*sz.pixf);

hLayout = siglayout.gridbaglayout(h.oframe);
hLayout.add(h.optimizezeros,       1, 1, ...
    'Fill', 'Horizontal', ...
    'TopInset',  15*sz.pixf, ...
    'LeftInset', 10*sz.pixf, ...
    'RightInset', 10*sz.pixf);
hLayout.add(h.optimizeones,        2, 1, ...
    'Fill', 'Horizontal', ...
    'LeftInset', 10*sz.pixf, ...
    'RightInset', 10*sz.pixf);
hLayout.add(h.optimizenegones,     3, 1, ...
    'Fill', 'Horizontal', ...
    'LeftInset', 10*sz.pixf, ...
    'RightInset', 10*sz.pixf);
hLayout.add(h.optimizedelaychains, 4, 1, ...
    'Fill', 'Horizontal', ...
    'LeftInset', 10*sz.pixf, ...
    'RightInset', 10*sz.pixf);

hLayout.add(h.optimizescalevalues, 5, 1, ...
    'Fill', 'Horizontal', ...
    'LeftInset', 10*sz.pixf, ...
    'RightInset', 10*sz.pixf);

l = [ ...
        handle.listener(this, this.findprop('Filter'), ...
        'PropertyPostSet', @lclfilter_listener); ...
        handle.listener(this, this.findprop('BlockName'), ...
        'PropertyPostSet', @blockname_listener); ...
    ];

set(l, 'CallbackTarget', this);
set(this, 'WhenRenderedListeners', union(this.WhenRenderedListeners, l));

lclfilter_listener(this);
blockname_listener(this);

setupenablelink(this, 'destination', 'user defined', 'userdefined', '-update');
setupenablelink(this, 'destination', {'current', 'user defined'}, 'overwriteblock', '-update');
setupenablelink(this, 'usebasicelements', 'on', 'optimizezeros', ...
    'optimizeones', 'optimizenegones', 'optimizedelaychains', '-update');

if isspblksinstalled
    postfix = [filesep 'dspblks'];
else
    postfix = [];
end
cshelpcontextmenu(this, ['fdatool_dspfwiz_panel' postfix]);

%-------------------------------------------------------------------
function lclfilter_listener(this, eventData)

Hd = get(this, 'Filter');
h  = get(this, 'Handles');

if ismethod(Hd, 'block') && isspblksinstalled,
    w = warning('off');
    try

        [wstr, wid] = lastwarn;
        blockparams(Hd, 'off');
        enabstate = this.Enable;
        lastwarn(wstr, wid);
    catch ME %#ok<NASGU>

        set(this, 'UseBasicElements', 'On');
        enabstate = 'off';
    end
    warning(w);
else
    set(this, 'UseBasicElements', 'On');
    enabstate = 'off';
end

if ~isrealizable(Hd)
    enabstate = 'off';
    set(this, 'UseBasicElements', 'Off');
end

setenableprop(h.usebasicelements, enabstate);

if isa(Hd, 'dfilt.abstractsos')
    enabstate = this.Enable;
else
    enabstate = 'off';
end

setenableprop(h.optimizescalevalues, enabstate);
prop_listener(this, 'optimizescalevalues');

%-------------------------------------------------------------------
function blockname_listener(this, eventData)

h = get(this, 'Handles');

set(h.overwriteblock, 'String', ...
    sprintf(get(findprop(this, 'OverwriteBlock'), 'Description'), this.BlockName));

% Adjust uicontrol size to fit new label, but don't let it extend outside the frame. 
%

% Cache old units.
origunits = get(h.overwriteblock,'Units');
set(h.overwriteblock,'Units','pixels');

% Get sizes and positions.
sz = gui_sizes(this);
w = largestuiwidth(h.overwriteblock);
w = w+sz.rbwTweak;
pos = get(h.overwriteblock,'Position');

% Set new position, but don't overrun the frame.
mframepos = getpixelpos(this,'mframe',1);
framewidth = mframepos(3);
if w > framewidth,   w = framewidth-2*sz.hfus; end
set(h.overwriteblock,'Position',[pos(1), pos(2), w, pos(4)]);

% Reset to original units.
set(h.overwriteblock,'Units',origunits);

% [EOF]
